<template>
  <div class="relative w-full h-full overflow-y-auto">
    <PageHeader :title="t('service.退款详情')">
      <template #right>
        <el-button type="primary" @click="goBack" size="small">
          <SvgIcon name="iconfanhui" color="#fff" class="mr4" />{{
            t("service.返回上级页面")
          }}
        </el-button>
      </template>
    </PageHeader>

    <Step :detail="detailInfo" @rest="getDetail" v-show="detailInfo.id" />

    <template
      v-if="detailInfo.refundAskType == 2 && detailInfo.refundCheckTime"
    >
      <div class="mt15 mb15 pl5 color-333333 font-700">{{ t('service.买家退货信息') }}:</div>
      <el-table
        :show-header="false"
        :data="sendInfo"
        style="width: 100%"
        :cell-class-name="cellClassName"
        border
        :span-method="arraySpanMethod"
      >
        <el-table-column prop="label1" />
        <el-table-column prop="value1" />
        <el-table-column prop="label2" />
        <el-table-column prop="value2" />
      </el-table>
    </template>

    <div class="mt15 mb15 pl5 color-333333 font-700">{{ t('service.退款信息') }}:</div>
    <el-table
      :show-header="false"
      :data="tableData"
      style="width: 100%"
      :cell-class-name="cellClassName"
      border
      :span-method="arraySpanMethod"
    >
      <el-table-column prop="label1" />
      <el-table-column prop="value1" />
      <el-table-column prop="label2" />
      <el-table-column prop="value2" />
    </el-table>

    <div class="mt15 mb15 pl5 color-333333 font-700">{{ t('service.退款凭证信息') }}:</div>
    <el-table
      :show-header="false"
      :data="tableData2"
      style="width: 100%"
      :cell-class-name="cellClassName"
      border
      :span-method="arraySpanMethod2"
    >
      <el-table-column prop="label1" />
      <el-table-column prop="value1">
        <template #default="scope">
          <div class="flex_aic" v-if="scope.row.value1">
            <div class="flex_aic gap-10" v-for="(img, index) in scope.row.value1.split(',')" :key="img">
              <video
                :src="img"
                :controls="false"
                style="width: 100px; height: 100px"
                v-if="img?.includes('.mp4')"
                @click="previewVideo(img)"
                class="flex-shrink-0"
              ></video>
              <el-image
                v-else
                style="width: 100px; height: 100px"
                :src="img"
                :zoom-rate="1.2"
                :max-scale="7"
                :min-scale="0.2"
                :preview-src-list="scope.row.value1.split(',')"
                show-progress
                :initial-index="4"
                fit="cover"
                preview-teleported
              />
            </div>
          </div>
          <span v-else>{{ t('service.暂无凭证') }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="label2" />
      <el-table-column prop="value2" />
    </el-table>

    <Kaipiao :orderDetail="detailInfo" />

    <div class="mt15 mb15 pl5 color-333333 font-700">{{ t('service.商品信息') }}:</div>
    <el-table
      :data="detailInfo.saleSkus"
      style="width: 100%"
      border
      class="cover-table"
    >
      <el-table-column :label="t('service.序号')" width="100" type="index" />
      <el-table-column :label="t('service.商品信息')" prop="info">
        <template #default="{ row }">
          <div class="flex_aic">
            <el-image
              style="width: 100px; height: 100px"
              :src="row.skuSpecDetailImage"
              :zoom-rate="1.2"
              :max-scale="7"
              :min-scale="0.2"
              :preview-src-list="[row.skuSpecDetailImage]"
              show-progress
              :initial-index="4"
              fit="cover"
              preview-teleported
              class="flex-shrink-0"
            />
            <div class="ml10 w-380 text-left">
              <div>{{ row["skuName" + locale] }}</div>
              <div>
                {{ row["skuClassify1Name" + locale] }} >
                {{ row["skuClassify2Name" + locale] }}
              </div>
              <div class="flex_aic">
                {{ t('service.规格') }}：
                <span class="goods_spec flex_aic gap-4 flex-wrap">
                  <span v-for="spec in row.skuSpecDetail" :key="spec.specId"
                    >{{ spec["specName" + locale] }}-{{
                      spec["specDetailName" + locale]
                    }}</span
                  >
                </span>
              </div>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column :label="t('service.商品单价')" prop="skuStockPrice" width="160">
        <template #default="{ row }">
          <span>￥{{ row.skuStockPrice }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="t('service.退款数量')" prop="refundSkuCount" width="160" />
      <el-table-column
        :label="t('service.商品运费')"
        prop="skuPostage"
        width="160"
        :formatter="(row) => '￥' + row.skuPostage"
      />
      <el-table-column :label="`${t('order_lists_detail.抵扣金额')}（${t('order_lists_detail.活动')}/${t('order_lists_detail.优惠券')}/${t('order_lists_detail.积分')}）`" width="320">
        <template #default="{ row }">
          <div>
            {{ row.payDiscountActivity }}/{{ row.payDiscountCoupon }}/{{
              row.payDiscountPoint
            }}
          </div>
          <div>{{ t('order_lists_detail.共计') }}：{{ row.payDiscount }}</div>
        </template>
      </el-table-column>
      <el-table-column
        :label="t('service.实付金额不含运费')"
        prop="payRealSku"
        width="160"
        :formatter="(row) => '￥' + row.payRealSku"
      />

      <!-- <el-table-column prop="refundRealSku" label="退款金额(元)" width="160">
        <template #default="{ row }">
          <span>￥{{ row.refundRealSku || "--" }}</span>
        </template>
      </el-table-column> -->
    </el-table>

    <!-- 视频预览对话框 -->
    <el-dialog v-model="showVideoDia" :title="$t('uploadVideo.视频预览')" width="800px">
      <div class="flex_aic_jcc w-full h-full">
        <video
          :src="videoSrc"
          controls
          style="max-width: 80%; max-height: 500px;"
          v-if="videoSrc"
        ></video>
      </div>
    </el-dialog>
  </div>
</template>
<script setup>
import { ref, onMounted, watchEffect } from "vue";
import { useRouter } from "vue-router";
import Api from "@/config/api/api";
import { useRoute } from "vue-router";
import { useI18n } from "vue-i18n";
import { refundTypeMap } from "../data";
import Step from "./step.vue";
import moment from "moment";
import Kaipiao from "../../order_lists_detail/components/kaipiao.vue";

const { locale, t } = useI18n();
const route = useRoute();
const router = useRouter();
const detailInfo = ref({});

// 退款信息
const tableData = ref([]);
// 退款凭证信息
const tableData2 = ref([]);
// 买家发货信息
const sendInfo = ref([]);
const showVideoDia = ref(false)
const videoSrc = ref("")
watchEffect(() => {
  tableData.value = [
    {
      label1: t('service.订单号'),
      value1: detailInfo.value.sourceSaleNo,
      label2: t('service.退款编号'),
      value2: detailInfo.value.saleNo,
    },
    {
      label1: t('service.退款金额元'),
      value1: detailInfo.value.refundReal,
      label2: t('service.退款原因'),
      value2: detailInfo.value["refundAskReasonName" + locale.value],
    },
    {
      label1: t('service.退款类型'),
      value1: t(`service.${refundTypeMap[detailInfo.value.refundAskType]}`),
      label2: t('service.会员'),
      value2: detailInfo.value.vipNickName,
    },
    {
      label1: t('service.补充说明'),
      value1: detailInfo.value.refundAskRemark,
    },
  ];

  tableData2.value = [
    {
      label1: t('service.退款凭证'),
      value1: detailInfo.value.refundAskSkuImage,
    },
  ];

  sendInfo.value = [
    {
      label1: t('service.快递单号'),
      value1: detailInfo.value.refundVipSendNo,
      label2: t('service.发货时间'),
      value2: detailInfo.value.refundVipSendTime
        ? moment(detailInfo.value.refundVipSendTime).format(
            "YYYY-MM-DD HH:mm:ss"
          )
        : "",
    },
    {
      label1: t('service.退货地址'),
      value1:
        detailInfo.value["refundCheckAddressProvince" + locale.value] +
        detailInfo.value["refundCheckAddressCity" + locale.value] +
        detailInfo.value["refundCheckAddressArea" + locale.value] +
        detailInfo.value["refundCheckAddressAddress" + locale.value],
      label2: t('service.收货人'),
      value2: detailInfo.value["refundCheckAddressName" + locale.value],
    },
    {
      label1: t('service.联系电话'),
      value1: detailInfo.value.refundCheckAddressPhone,
    },
  ];
});

const previewVideo = (src)=>{
  showVideoDia.value = true
  videoSrc.value = src
}
const cellClassName = ({ row, column, rowIndex, columnIndex }) => {
  if (columnIndex % 2 === 0) {
    return "warning-column winth-20";
  } else {
    return "winth-30";
  }
};

const arraySpanMethod = ({ row, column, rowIndex, columnIndex }) => {
  if (
    (columnIndex === 2 && rowIndex === 3) ||
    (columnIndex === 3 && rowIndex === 3)
  ) {
    return [0, 0];
  } else if (columnIndex === 1 && rowIndex === 3) {
    return [1, 3];
  } else {
    return [1, 1];
  }
};

const arraySpanMethod2 = ({ row, column, rowIndex, columnIndex }) => {
  if (columnIndex === 1 && rowIndex === 0) {
    return [1, 3];
  } else {
    return [1, 1];
  }
};

const valueFormatter = (row, column) => {
  console.log(row, column);
  return detailInfo.value[column.property];
};

const goBack = () => {
  router.back();
};

const getDetail = () => {
  Api.getOrderDetail(route.query.id).then((res) => {
    detailInfo.value = res.data;
  });
};

onMounted(() => {
  getDetail();
});
</script>
<style lang="scss" scoped>
.mt50 {
  margin-top: 50px;
}

:deep(.warning-column) {
  background-color: #f8f9fa;
}
:deep(.winth-20) {
  width: 20%;
}
:deep(.winth-30) {
  width: 30%;
}
.w-380 {
  width: 380px;
}
</style>
